<?php
/**
 * RedCross Application
 *
 * @copyright Copyright (c) 2010, Kristian Stokke Tryggestad, Rune Vikestad, Andreas Flaten Wist, Simen Nicolai Echholt, Ole Henrik Jahren
 */

/**
 * Validator for the time of shifts
 * 
 * 	@category  	RedCross
 * 	@package	RedCross_Validate
 */
class RedCross_Validate_DatetimeShift extends Zend_Validate_Abstract
{
	const INVALID_DATE = 'datetimeInvalidDate';
	const INVALID_TIME = 'datetimeInvalidTime';
	const END_BEFORE_START = 'datetimeEndBeforeStart';
	
	protected $_messageTemplates = array(
		self::INVALID_DATE 	=> "'%value%' -- er ikke en gyldig dato",
		self::INVALID_TIME => "'%value%' -- er ikke et gyldig tidspunkt",
		self::END_BEFORE_START => "Oppmøte tiden for arrangementet er etter start tiden."
	);
	
	public function isValid($value, $context = null)
	{
		//check the date  2010-04-13 12:00:00
		$year = substr($value,0,4);
		$month = substr($value,5,2);
		$day = substr($value,8,2);
		$hour = substr($value,11,2);
		$minutes = substr($value,14,2);

		if (!checkdate((int)$month,(int)$day,(int)$year)){
			$this->_setValue($day . '.' . $month . '.' . $year);
			$this->_error(self::INVALID_DATE);
			return false;
		} 
		
		if (!(((int)$hour >= 0) && ((int)$hour <= 23))){
			$this->_setValue($hour . ':' . $minutes);
			$this->_error(self::INVALID_TIME);
			return false;
		} else {
			if(!(((int)$minutes >= 0) && ((int)$minutes <= 59))){
				$this->_setValue($hour . ':' . $minutes);
				$this->_error(self::INVALID_TIME);
				return false;
			}
		}
		
		// check that the start date/time is before the end date/time
		if (is_array($context)) {
			if (isset($context['appearance_time'])){
				
				$date = $context['appearance_time'];				
				$start_date_year = $date['year'];
				$start_date_month = $date['month'];
				$start_date_day = $date['day'];
				$start_date_hour = $date['hours'];
				$start_date_minutes = $date['minutes'];
				
				if((int)$year < (int)$start_date_year) {
					$this->_setValue(' år ' . $start_date_year . ' er etter år ' . $year);
					$this->_error(self::END_BEFORE_START);
					return false;
				}
			
				
				if((int)$year == (int)$start_date_year) {
					if((int)$month < (int)$start_date_month) {
						$this->_setValue(' måned ' . $start_date_month . ' er etter måned ' . $month);
						$this->_error(self::END_BEFORE_START);
						return false;
					}
				}
				
				if(((int)$year == (int)$start_date_year) && ((int)$month == (int) $start_date_month)) {
					if((int)$day < (int)$start_date_day) {
						$this->_setValue(' dato ' . $start_date_day . ' er etter dato ' . $day);
						$this->_error(self::END_BEFORE_START);
						return false;
					}
				}
				
				if(((int)$year == (int)$start_date_year) && ((int)$month == (int) $start_date_month) && ((int)$day == (int)$start_date_day)) {
					if((int)$hour < (int)$start_date_hour) {
						$this->_setValue(' timen ' . $start_date_hour . ' er etter timen ' . $hour);
						$this->_error(self::END_BEFORE_START);
						return false;
					}
				}
				
				if(((int)$year == (int)$start_date_year) && ((int)$month == (int) $start_date_month) && ((int)$day == (int)$start_date_day) && ((int) $hour == (int) $start_date_hour)) {
					if((int)$minutes < (int)$start_date_minutes) {
						$this->_setValue(' minutt ' . $start_date_minutes . ' er etter minutt ' . $minutes);
						$this->_error(self::END_BEFORE_START);
						return false;
					}
				}
				
				
			}
		}
		
		return true;
	}
	
}